package edu.njfu.networkdemo.kotinlearning

import android.provider.Settings
import kotlinx.coroutines.*

fun main() {
    runBlocking {
        launch {
           for(i in 1..10) {
               println("coroutine1" + i)
           }
        }
        launch {
            for(i in 1..10) {
                println("coroutine2" + i)
            }
        }

        test()
        printDot()
    }

    //1. 创建一个Job
    var job=Job()
    //2. 创建CoroutineScope对象
    var scope=CoroutineScope(job)
    //3. 调用launch函数
    scope.launch{
        //逻辑处理
        print("hello world!")
    }
    job.cancel()
    val start=System.currentTimeMillis()
    runBlocking {
        val result1=async {
            delay(1000)
            5+5
        }
        val result2=async {
            delay(1000)
            3+4
        }
        println(result1.await() + result2.await())
        val end=System.currentTimeMillis()
        println(end - start)
    }

}

/**
 * 把函数test声明为suspend,挂起函数，不能直接运行，必须在协程作用域内运行
 */
suspend fun test(){
    delay(1000)
    println("hello world!")
}

suspend fun  printDot()= coroutineScope {
    launch {
        delay(1000)
        println("hello")
    }
}

